package com.timothy.junk.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	
//	public static final String MY_DATA_FORMAT = "yyyy-MM-dd";
//	public static final String MY_DATA_FORMAT2 = "yyyy-MM-dd hh:mm:ss";
	public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd");
	public static final SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

	public static String getNowDate() {
	   Date currentTime = new Date();
	   return dateFormat.format(currentTime);
	}
	
	public static String getNowTimeStr() {
	   Date currentTime = new Date();
	   return timeFormat.format(currentTime);
	}
	
	public static Date getNowTime() {
	   return new Date();
	}
	
	public static Date getTime(int hour,int minute) {
		String nowDate = getNowDate();
		String hourStr = String.valueOf(hour);
		hourStr = hourStr.length() == 1? "0"+hourStr:hourStr;
		String minuteStr = String.valueOf(minute);
		minuteStr = minuteStr.length() == 1? "0"+minuteStr:minuteStr;	
		Date sDate = null;
		try {
			sDate = timeFormat.parse(nowDate +" " + hourStr +":" + minuteStr +":00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return sDate;
	}
	
	public static int compareDate(String date1Str, String date2Str) {
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = dateFormat.parse(date1Str);
			date2 = dateFormat.parse(date2Str);
			if (date1.getTime() > date2.getTime()) {
				return 1;
			} else if (date1.getTime() < date2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * transfer date from "yyyyMMdd" to "yyyy-MM-dd"
	 * @param date2
	 * @return
	 */
	public static String dateTransfer(String date2){
		Date dateStr2;
		String dateStr = null;
		try {
			dateStr2 = dateFormat2.parse(date2);
			dateStr = dateFormat.format(dateStr2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 
		return dateStr;
	}
	
	public static Integer getDayBetween(String startDateStr,String endDateStr){
		Date startDate = null;
		Date endDate = null;
		
		try {
			if(startDateStr.length() == 10){
				startDate = dateFormat.parse(startDateStr);
			}else if(startDateStr.length() == 8){
				startDate = dateFormat2.parse(startDateStr);
			}
			
			if(endDateStr.length() == 10){
				endDate = dateFormat.parse(endDateStr);
			}else if(startDateStr.length() == 8){
				endDate = dateFormat2.parse(endDateStr);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if(startDate == null || endDate == null){
			return null;
		}else{
			int days = (int) getDaysBetween(startDate,endDate).longValue();
			return Integer.valueOf(days);
		}
		
	}
	
	public static Long getDaysBetween(Date startDate, Date endDate) {
		Calendar fromCalendar = Calendar.getInstance();
		fromCalendar.setTime(startDate);
		fromCalendar.set(Calendar.HOUR_OF_DAY, 0);
		fromCalendar.set(Calendar.MINUTE, 0);
		fromCalendar.set(Calendar.SECOND, 0);
		fromCalendar.set(Calendar.MILLISECOND, 0);

		Calendar toCalendar = Calendar.getInstance();
		toCalendar.setTime(endDate);
		toCalendar.set(Calendar.HOUR_OF_DAY, 0);
		toCalendar.set(Calendar.MINUTE, 0);
		toCalendar.set(Calendar.SECOND, 0);
		toCalendar.set(Calendar.MILLISECOND, 0);

		return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / (1000 * 60 * 60 * 24);
	}


}
